package org.misalen.web.database.repository.mysql;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.misalen.web.database.dao.mysql.impl.BaseMySqlDaoImpl;
import org.misalen.web.database.model.mysql.sys.SysWordbookItem;
import org.springframework.stereotype.Repository;

@Repository
public class SysWordbookItemRepository extends BaseMySqlDaoImpl<SysWordbookItem> {

	@SuppressWarnings("unchecked")
	public List<SysWordbookItem> findByWbId(String wordbookId) {
		Session session = this.openSession();
		Criteria criteria = session.createCriteria(SysWordbookItem.class, "a");
		criteria.setCacheable(true);
		criteria.add(Restrictions.eq("wordbookId", wordbookId));
		List<SysWordbookItem> items =criteria.list();
		closeSession(session);
		return items;
	}

	@SuppressWarnings("unchecked")
	public List<SysWordbookItem> findByWbIdAndName(String wordbookId, String itemName) {
		Session session = this.openSession();
		Criteria criteria = session.createCriteria(SysWordbookItem.class, "a");
		criteria.setCacheable(true);
		criteria.add(Restrictions.eq("wordbookId", wordbookId));
		criteria.add(Restrictions.eq("itemName", itemName));
		List<SysWordbookItem> items =criteria.list();
		closeSession(session);
		return items;
	}

	public SysWordbookItem getByCode(String wdItemCode) {
		Session session = this.openSession();
		Criteria criteria = session.createCriteria(SysWordbookItem.class, "a");
		criteria.setCacheable(true);
		criteria.add(Restrictions.eq("itemCode", wdItemCode));
		SysWordbookItem item =	(SysWordbookItem) criteria.list().get(0);
		closeSession(session);
		return item;
	}
}